import heuristics.NoHeuristicFunction;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import aima.core.agent.Action;
import aima.core.search.framework.GraphSearch;
import aima.core.search.framework.Problem;
import aima.core.search.framework.Search;
import aima.core.search.framework.SearchAgent;
import aima.core.search.framework.TreeSearch;
import aima.core.search.informed.AStarSearch;
import aima.core.search.uninformed.BreadthFirstSearch;
import aima.core.search.uninformed.DepthFirstSearch;

public class Application {

	private static final String FILE_PATH = "files\\kruimelMini.txt";

	public static void main(String[] args) {
		
		char[][] map = TextFileReader.getMap(FILE_PATH);
		/*
		randomRoosterGeneratie rooster= new randomRoosterGeneratie(7);
		rooster.generateRooster();
		char[][] map= rooster.getRooster();
		*/
		PacmanBoard pacmanBoard = new PacmanBoard(map);
		PacmanMap pacmanMap = new PacmanMap(pacmanBoard); // = state
		
		System.out.println("Before search:" );
		pacmanMap.printMap();
		
		//pacmanBreadthFirstGraphSearch(board);
		pacmanBreadthFirstTreeSearch(pacmanMap);
	}
	
	private static void pacmanBreadthFirstTreeSearch(PacmanMap pacmanMap) {
		Problem problem = new Problem(pacmanMap, new PacmanActionsFunction(), new PacmanResultFunction(), new PacmanGoalTest());
		Search search = new BreadthFirstSearch(new TreeSearch());
		performSearch(problem, search);
	}
	
	private static void pacmanBreadthFirstGraphSearch(PacmanMap pacmanMap) {
		Problem problem = new Problem(pacmanMap, new PacmanActionsFunction(), new PacmanResultFunction(), new PacmanGoalTest());
		Search search = new BreadthFirstSearch(new GraphSearch());
		performSearch(problem, search);
	}
	
	private static void pacmanDepthFirstGraphSearch(PacmanMap pacmanMap) {
		Problem problem = new Problem(pacmanMap, new PacmanActionsFunction(), new PacmanResultFunction(), new PacmanGoalTest());
		Search search = new DepthFirstSearch(new GraphSearch());
		performSearch(problem, search);
	}
	
	private static void pacmanAStarSearch(PacmanMap pacmanMap) {
		Problem problem = new Problem(pacmanMap, new PacmanActionsFunction(), new PacmanResultFunction(), new PacmanGoalTest());
		Search search = new AStarSearch(new GraphSearch(), new NoHeuristicFunction());
		performSearch(problem, search);
	}
	
	private static void performSearch(Problem problem, Search search) {
		SearchAgent agent;
		
		try {
			
			agent = new SearchAgent(problem, search);
			//System.out.println("komaan");
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
			
		} catch (Exception e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void printInstrumentation(Properties properties) {
		System.out.println("\nPrinting instrumentation: ");
		Iterator<Object> keys = properties.keySet().iterator();
		while (keys.hasNext()) {
			String key = (String) keys.next();
			String property = properties.getProperty(key);
			System.out.println(key + " : " + property);
		}

	}

	private static void printActions(List<Action> actions) {
		System.out.println("\nPrinting actions: ");
		for (int i = 0; i < actions.size(); i++) {
			String action = actions.get(i).toString();
			System.out.println(action);
		}
	}

}